#include "MyLogeer.h"
#include "Configuration.h"

#include <iostream>
using std::cout;
using std::endl;
using std::string;

using namespace log4cpp;

MyLogger *MyLogger::_pInstance = nullptr;
MyLogger::AutoRelease MyLogger::_ar;
MyLogger::MyLogger()
    : _cat(log4cpp::Category::getInstance("zh"))
{
    cout << "MyLogger()" << endl;
    // 1.创建日志布局对象
    auto ptn1 = new PatternLayout();
    ptn1->setConversionPattern("%d %c [%p] %m%n");
    auto ptn2 = new PatternLayout();
    ptn2->setConversionPattern("%d %c [%p] %m%n");

    // 2.创建日志目的地对象
    auto pOsAppender = new OstreamAppender("console", &cout);
    pOsAppender->setLayout(ptn1);
    // 从配置文件中获取日志存放路径
    string logfile = Configuration::getinstance()->get()["log_file"];
    cout << "set log file:" << logfile << endl;
    auto pFileAppender = new FileAppender("fileAppender", logfile);
    pFileAppender->setLayout(ptn2);

    // 3.设置Category对象
    _cat.setPriority(Priority::DEBUG);
    _cat.addAppender(pOsAppender);
    _cat.addAppender(pFileAppender);
}
MyLogger::~MyLogger()
{
    cout << "~MyLogger()" << endl;
    Category::shutdown();
}
void MyLogger::error(const char *msg)
{
    _cat.error(msg);
}
void MyLogger::warn(const char *msg)
{
    _cat.warn(msg);
}

void MyLogger::debug(const char *msg)
{
    _cat.debug(msg);
}

void MyLogger::info(const char *msg)
{
    _cat.info(msg);
}